package lambdasinaction.chap3;

import java.io.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExecuteAround {

	public static void main(String ...args) throws IOException{

        // method we want to refactor to make more flexible
//        String result = processFileLimited();
//        System.out.println(result);
//
//        System.out.println("---");
//
//		String oneLine = processFile((BufferedReader b) -> b.readLine());
//		System.out.println(oneLine);
//
//		String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
//		System.out.println(twoLines);
//
//        processFileLimited1();
        List<String> list = processFileLimited2();

        processFileLimited3(list);


    }

    public static String processFileLimited() throws IOException {
        try (BufferedReader br =
                     new BufferedReader(new FileReader("C:\\Users\\29328\\Desktop\\abc\\笔记\\111\\WpBaseInfoMapper.xml"))) {
            return br.readLine();
        }
    }


    public static List<String> processFileLimited2() throws IOException {
        try (BufferedReader br =
                     new BufferedReader(new FileReader("C:\\Users\\29328\\Desktop\\abc\\笔记\\111\\WpBaseInfoMapper.xml"))) {
            List<String> stringList = br.lines().collect(Collectors.toList());
            return stringList;
        }
    }

    public static void processFileLimited3(List<String> stringList) throws IOException{
	    try(
	        BufferedWriter br=new BufferedWriter(new FileWriter("C:\\Users\\29328\\Desktop\\abc\\笔记\\111\\WpBaseInfoMapper.txt"))){
	        stringList.forEach((String str) -> {
                try {
                    br.write(str+"\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }

            });

        }
    }




	public static String processFile(BufferedReaderProcessor p) throws IOException {
		try(BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\29328\\Desktop\\abc\\笔记\\111\\WpBaseInfoMapper.xml"))){
			return p.process(br);
		}

	}

	public interface BufferedReaderProcessor{
		public String process(BufferedReader b) throws IOException;

	}
}
